package fr.mehfile.timetable;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import fr.mehfile.view.utils.Utils;

import android.content.Context;
import android.widget.Toast;

public class TimeTableAlarmManager {

	private boolean fajr = false;
	private boolean leverSoleil = true;
	private boolean zohr = false;
	private boolean coucherSoleil = true;
	private boolean magrib = false;
	private TimeTableModel timeTableModel;
	
	private Context context;

	public TimeTableAlarmManager(Context context, TimeTableModel timeTableModel){
		this.context = context;
		this.timeTableModel = timeTableModel;
		//this.writeSettings(context, "0;1;1;1;0");
		String result = "0;0;0;0;0";
		try {
			result = this.readSettings(context);
		} catch (IOException e) {
			this.writeSettings(context, "0;0;0;0;0");
			try {
				result = this.readSettings(context);
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}

		Utils.debugger.logDebug("ttam", "avant tokens");
		String[] tokens = result.split(";");
		this.fajr = (Integer.parseInt(tokens[0]) != 0); 
		this.leverSoleil = (Integer.parseInt(tokens[1]) != 0); 
		this.zohr = (Integer.parseInt(tokens[2]) != 0); 
		this.coucherSoleil = (Integer.parseInt(tokens[3]) != 0); 
		this.magrib = (Integer.parseInt(tokens[4]) != 0); 

		Utils.debugger.logDebug("ttam", "contenu de fajr : " + this.fajr);
		Utils.debugger.logDebug("ttam", "contenu de leverSoleil : " + this.leverSoleil);
		Utils.debugger.logDebug("ttam", "contenu de zohr : " + this.zohr);
		Utils.debugger.logDebug("ttam", "contenu de coucherSoleil : " + this.coucherSoleil);
		Utils.debugger.logDebug("ttam", "contenu de magrib : " + this.magrib);
	}
	
	public String readSettings(Context context) throws IOException{ 
		Utils.debugger.logDebug("ttam", "debut readSettings");
		FileInputStream fIn = null; 
        InputStreamReader isr = null; 
 
        char[] inputBuffer = new char[9]; 
        String data = null; 
 
       	fIn = context.openFileInput("timetable.dat");       
        isr = new InputStreamReader(fIn); 
        isr.read(inputBuffer); 
        data = new String(inputBuffer); 

        
        
        return data;
		
	}
	
	public void writeSettings(Context context, String data){ 
		Utils.debugger.logDebug("ttam", "debut writeSettings");
        FileOutputStream fOut = null; 
        OutputStreamWriter osw = null; 
 
        try{ 
           fOut = context.openFileOutput("timetable.dat",Context.MODE_PRIVATE);       
            osw = new OutputStreamWriter(fOut); 
            osw.write(data); 
            osw.flush(); 
            
            } 
            catch (Exception e) {       
                    e.printStackTrace();
            } 
            finally { 
               try { 
                      osw.close(); 
                      fOut.close(); 
                      } catch (IOException e) { 
                               Toast.makeText(context, "Settings not saved",Toast.LENGTH_SHORT).show(); 
                      } 
            } 
       }

	public boolean isFajr() {
		return fajr;
	}

	public void setFajr(boolean fajr) {
		this.fajr = fajr;
		this.saveData();
	}

	public boolean isLeverSoleil() {
		return leverSoleil;
	}

	public void setLeverSoleil(boolean leverSoleil) {
		this.leverSoleil = leverSoleil;
		this.saveData();
	}

	public boolean isZohr() {
		return zohr;
	}

	public void setZohr(boolean zohr) {
		this.zohr = zohr;
		this.saveData();
	}

	public boolean isCoucherSoleil() {
		return coucherSoleil;
	}

	public void setCoucherSoleil(boolean coucherSoleil) {
		this.coucherSoleil = coucherSoleil;
		this.saveData();
	}

	public boolean isMagrib() {
		return magrib;
	}

	public void setMagrib(boolean magrib) {
		this.magrib = magrib;
		this.saveData();
	}
	
	private void saveData() {
		String fajr;
		String leverSoleil;
		String zohr;
		String coucherSoleil;
		String magrib;
		
		if(this.fajr)
			fajr = "1";
		else
			fajr = "0";
		
		if(this.leverSoleil)
			leverSoleil = "1";
		else
			leverSoleil = "0";
		
		if(this.zohr)
			zohr = "1";
		else
			zohr = "0";
		
		if(this.coucherSoleil)
			coucherSoleil = "1";
		else
			coucherSoleil = "0";
		
		if(this.magrib)
			magrib = "1";
		else
			magrib = "0";
				
		
		String save = fajr + ";" + leverSoleil + ";" + zohr + ";" + coucherSoleil + ";" + magrib;
		this.writeSettings(this.context, save);
		this.planifierAllAlarmes();
	}

	private void planifierAllAlarmes() {
		TimeTableModel modelToday;
		TimeTableModel modelTomorrow;
		if(this.timeTableModel.isToday()){
			modelToday = this.timeTableModel;
			modelTomorrow = new TimeTableModel("tomorrow");
		}
		else{
			modelToday = new TimeTableModel("today");
			modelTomorrow = this.timeTableModel;
		}
		
		if(this.fajr){
			planifierNextAlarme(modelToday.getFajr(), modelTomorrow.getFajr());
		}
		if(this.leverSoleil){
			planifierNextAlarme(modelToday.getLeverDuSoleil(), modelTomorrow.getLeverDuSoleil());
		}
		if(this.zohr){
			planifierNextAlarme(modelToday.getZohr(), modelTomorrow.getZohr());
		}
		if(this.coucherSoleil){
			planifierNextAlarme(modelToday.getCoucherDuSoleil(), modelTomorrow.getCoucherDuSoleil());
		}
		if(this.magrib){
			planifierNextAlarme(modelToday.getMagrib(), modelTomorrow.getMagrib());
		}
	}

	private void planifierNextAlarme(String fajr2, String fajr3) {
		java.text.SimpleDateFormat formater = new java.text.SimpleDateFormat();
		//String month = formater.format(this.dayTimeTable);
	}

}
